<template>
  <view>
    <view class="input-search">
    	<uni-easyinput prefixIcon="search"  trim="all" v-model="keyword" :styles="styles" :placeholderStyle="placeholderStyle"></uni-easyinput>
    	<button @click="search">搜索</button>
    </view>
    <uni-table ref="table" type="" stripe :loading="loading" border emptyText="该仓库暂无更多数据" filter-type="search">
		<uni-tr v-for="(item, index) in card_arr" :key="index" >
			<uni-td width="100%" align="left" class="td-class" >
				<div @click="selectes(item)">
					<view class="FBillNo-bgcolor">
						<text class="font-red">位置：{{ item.name }}</text>
					</view>
					<view>
						<text>编码：{{ item.number }}</text>
					</view>
					<view>
						<text>类型：{{ item.type==1?'库区':'货架' }}</text>
						<text class="font-red">{{ item.ifempty==1?'空置':'' }}</text>
					</view>
					<view>
						<text>备注：{{ item.remark }}</text>
					</view>
				</div>
			</uni-td>
        </uni-tr>
    </uni-table>
    <view class="uni-pagination-box">
		<uni-pagination show-icon :page-size="pageSize" :current="currentPage"
        :total="total" @change="change" />
	</view>
	<uni-popup ref="popup" :animation="true" type="bottom" :maskClick="true">
		<view class="popup-content">
			
			<view class="pupup-search">
				<view class="left">类型：</view>
				<view class="right">
					<uni-data-checkbox style="margin:10rpx 0 0 20rpx" v-model="type" :localdata="type_arr"></uni-data-checkbox>
				</view>
			</view>
			<view class="pupup-search">
				<view class="left">空置：</view>
				<view class="right">
					<uni-data-checkbox style="margin:10rpx 0 0 20rpx" v-model="ifempty" :localdata="empty_type_arr"></uni-data-checkbox>
				</view>
			</view>
			<view class="pupup-search" v-if="row_column_show">
				<view class="left">货架号、行号：</view>
				<view class="right">
					<picker v-model="selectedOptions" :range="row_arr" @columnchange="bindPickerChange" mode="multiSelector">
						<uni-easyinput :inputBorder="false" suffixIcon="right" :value="row_colmn_str" disabled></uni-easyinput>
					</picker> 
				</view>
			</view>
			<view class="form_list">
				<view class="input-search"><button @click="closePup">确 定</button></view>
			</view>
		</view>
	</uni-popup>
	<!-- <scanCode :scanType="1" ref="scan" v-if="loadScanCode"></scanCode> -->
  </view>
</template>

<script>
// 引入uni-ui组件
import {
  uniPagination,
  uniIcons,
  uniCard,
  uniTable,
  uniTr,
  uniTh,
  uniTd,
  uniEasyinput,
  uniPopup,
  uniDataCheckbox
} from "@dcloudio/uni-ui"
//import scanCode from "@/component/scan-code.vue"
export default {
  data() {
    return {
      card_arr: [],//当前页码的表格数据
      pageSize: 15,// 每页数据量
      currentPage: 1,// 当前页
      total: 0,// 数据总量
      loading: false,
	  
      flag: '',
      index: '',
	  
	  barcode:'',
	  placeholderStyle:'font-size:17px;',
	  styles:{
	  	borderColor: '#999',
	  },
	  keyword:'',
	  where:' 1=1 ',
	  stock_id:uni.getStorageSync('user').stock_id, //默认
	  
	  type_arr: [
		  {
		  	text: '全部',
		  	value: 0
		  },
		{
			text: '货架',
			value: 2
		},{
			text: '库区',
			value: 1
		}],
	   type:0,
	   empty_type_arr: [
		  {
			text: '全部',
			value: ''
		  },
		{
			text: '空置',
			value: 1
		},{
			text: '非空',
			value: 0
		}],
		ifempty:'',
	   user:uni.getStorageSync('user'),
	   
	   row_arr: [['全部'],['全部']],
	   row_colmn_str:'请选择',
	   selectedOptions: [0, 0],
	   row_select:0,
	   column_select:0,
	   row_column_show:false,
	   from:'',//来自表头还是表体
	   inv_number:'',
	   cBatch:'',
	   //loadScanCode:false // 是否加载扫描组件
	   
	   //在线版
	   config:uni.getStorageSync('myconfig'),
	   pageinfo:{},
	   //
    }
  },
  components: {
    uniPagination,
    uniIcons, //uni-ui的icons组件
    uniTable,
    uniTr,
    uniTh,
    uniTd,uniDataCheckbox,
	uniEasyinput,uniPopup//,scanCode
  },
	onNavigationBarButtonTap(e) {
		if(e.type=='menu'){
			//弹窗
			this.$refs.popup.open()
		}
   },
   watch:{
	   type(newval){
		   if(newval==2){//货架
			   this.row_column_show = true
		   }else{
			   this.row_column_show = false
		   }
		   if(newval==0){
			   this.row_select = 0
			   this.column_select = 0
		   }
	   }
   },
   destroyed() {
   	this.globalData.toSecPage = ''
   },
   onPullDownRefresh() {
   	this.search()
   	uni.stopPullDownRefresh();
   },
  methods: {
    // 点击关闭的事件回调
    close() {
      uni.navigateBack({})
    },
	closePup(){
		this.$refs.popup.close()
		this.getlist();
	},
	// 搜索
	search() {
		this.getlist();
	},
    //获取列表
    async getlist() {
		if(!this.config.online){
			uni.showLoading();
			if(this.keyword!=''){
				this.where = " (number like '%"+this.keyword+"%' or name like '%"+this.keyword+"%' or remark like '%"+this.keyword+"%')"
			}else{
				this.where = ' 1=1 '
			}
			
			this.where += " and warehouse_id = "+this.stock_id
			
			if(this.type>0){
				this.where += " and type="+this.type
			}
			
			if(this.ifempty===1||this.ifempty===0){
				this.where += " and ifempty="+this.ifempty
			}
			if(this.type==2&&this.row_select>0){
				this.where += " and row_number="+this.row_select
			}
			if(this.type==2&&this.column_select>0){
				this.where += " and column_number="+this.column_select
			}
			if(this.inv_number!=''){
				let shelves_ids_arr = []
				await this.$Db.getKcShelvesIdsByInvNumber(this.inv_number).then(res=>{
					shelves_ids_arr = res
				}).catch(err=>{
					this.$Db.addlog('拣货获取物料所有货架',err)
				})
				
				if(shelves_ids_arr.length>0){
					this.where += " and id in ("+shelves_ids_arr.join(',')+")"
				}else{
					this.$Api.msg('暂无库存记录')
					return
				}
			}
			
			this.$Db.queryCount('shelves',this.where).then(res => {
				this.total = res[0].num;//获得总页数
			})
			this.$Db.queryDataList('shelves',this.currentPage,this.pageSize,this.where).then(res => {
				this.card_arr = res;
				uni.hideLoading();
			})
		}else{
			//20231204在线版
			
			//用于发货查询
			if(this.inv_number!=''){
				let shelves_ids_arr = []
				await this.$Db.getKcShelvesIdsByInvNumber(this.inv_number,this.cBatch).then(res=>{
					shelves_ids_arr = res
				}).catch(err=>{
					this.$Db.addlog('拣货获取物料所有货架',err)
				})
				
				if(shelves_ids_arr.length>0){
					this.where += " and id in ("+shelves_ids_arr.join(',')+")"
				}else{
					this.$Api.msg('暂无库存记录')
					return
				}
			}
			
			this.pageinfo = {
				table:'shelves',
				pagesize:this.pageSize,
				page:this.currentPage,
				order_str:'number asc',
				condition_str:JSON.stringify({
				  'keyword':this.keyword,
				  'warehouse_id':this.stock_id,
				  'type':this.type,
				  'ifempty':this.ifempty,
				  'row_number':this.row_select,
				  'column_number':this.column_select
				})
			}
			this.$Api.getPageList(this.pageinfo).then(res=>{
				this.card_arr = res.data.data;
				this.total = res.data.total
				this.currentPage = res.data.current_page
				uni.hideLoading();
			}).catch(err=>{
				this.$Api.msg(err)
			})
			//
		}
    },
    change(e) {
      this.currentPage = e.current
      this.getlist()
    },
	selectes(item) {
	  if (item == '') return
	  if(this.flag == 'add'){
		  //监听
		  uni.navigateBack({
			delta: 1,
			success: () => {
				if(this.from=='head'){
					uni.$emit("getShelvesHead", item)
				}else{
					uni.$emit("getShelves", item,this.index)
				}
			}
		  });
	  }
	  
	  //盘点
	  if(this.flag == 'check'){
		   uni.navigateBack({
			delta: 1,
			success: () => {
			  uni.$emit("get_Shelves", item)
			}
		  });
	  }
	  
	  //调拨
	  if(this.flag == 'allot'){
		   uni.navigateBack({
			delta: 1,
			success: () => {
			  uni.$emit("get_ToShelves", item)
			}
		  });
	  }
	},
	bindPickerChange: function(e) {
		//行变化
		if(e.target.column == 0){
			//选择行
			let row_value = this.row_arr[0][e.target.value]
			this.row_select = e.target.value
			
			//初始化行数组
			this.row_arr[1].splice(0, this.row_arr[1].length);
			this.row_arr[1].push('全部')
			this.selectedOptions[0] = e.target.value
			this.selectedOptions[1] = 0 //列归全部
			this.row_colmn_str = row_value
			
			if(row_value=='全部'){
				
			}else{
				let row_num = row_value.slice(1,-1)
				if(!isNaN(row_num)&&row_num>0){
					uni.showLoading({
						mask:true
					})
					
					if(!this.config.online){
						this.$Db.getShelvesColumn(row_num,this.stock_id).then(res=>{
							if(res.length>0){
								res.forEach(item=>{
									this.row_arr[1].push(item.column_str)
								})
							}
							uni.hideLoading()
						}).catch(err=>{
							uni.hideLoading()
							this.$Api.msg(err.message)
						})
					}else{
						this.pageinfo = {
							table:'shelvesColumns',
							pagesize:0,
							page:0,
							order_str:'column_number asc',
							condition_str:JSON.stringify({
							  'warehouse_id':this.stock_id,
							  'type':this.type,
							  'row_number':row_num
							})
						}
						this.$Api.getPageList(this.pageinfo).then(res=>{
							res.data.forEach(item=>{
								this.row_arr[1].push(item.column_str)
							})
							uni.hideLoading()
						}).catch(err=>{
							this.$Api.msg(err)
							uni.hideLoading()
						})
					}
				}
			}
		}else{
			//选择列
			let colmn_value = this.row_arr[1][e.target.value]
			this.column_select = e.target.value
			this.selectedOptions[1] = e.target.value
			
			if(colmn_value!='全部'){
				this.row_colmn_str = this.row_arr[0][this.row_select]+','+colmn_value
			}else{
				this.row_colmn_str = this.row_arr[0][this.row_select]
			}
		}
	},
	/* 获取行号 */
	getRowArr(type){
		if(!this.config.online){
			this.$Db.getShelvesRow(type,this.stock_id).then(res=>{
				if(res.length>0){
					res.forEach(item=>{
						this.row_arr[0].push(item.row_str)
					})
				}
			}).catch(err=>{
				//this.$Api.msg(err.message)
				this.$Api.msg('请先同步数据')
			})
		}else{
			this.pageinfo = {
				table:'shelvesRows',
				pagesize:this.pageSize,
				page:this.currentPage,
				order_str:'row_number asc',
				condition_str:JSON.stringify({
				  'warehouse_id':this.stock_id,
				  'type':this.type
				})
			}
			this.$Api.getPageList(this.pageinfo).then(res=>{
				res.data.forEach(item=>{
					this.row_arr[0].push(item.row_str)
				})
			}).catch(err=>{
				this.$Api.msg(err)
			})
		}
	}
  },
  mounted(e) {},
  onLoad(e) {
	this.flag = e.flag
	this.index = e.index
	this.type = e.type
	this.from = e.from
	
	//更新标题
	this.$Func.setBarTitle('库区、货架')
	
	if(e.stock_id!=undefined&&!isNaN(e.stock_id)){
		this.stock_id = e.stock_id
	}
	
	//拣货选货架时用到
	if(e.inv_number!=undefined){
		this.inv_number = e.inv_number
	}
	if(e.cBatch!=undefined){
		this.cBatch = e.cBatch
	}
	
	this.getlist()
	this.getRowArr(2)
	
	//if(this.flag == 'add'){
		/* 监听物料扫码 */
		uni.$off("scan_shelves")
		uni.$on("scan_shelves", (res) => {
			//货架、库区
			if(this.type != undefined){
				if(res.type!=this.type&&this.type==1){
					this.$Api.msg('请扫库区二维码')
					return 
				}
				if(res.type!=this.type&&this.type==2){
					this.$Api.msg('请扫货架二维码')
					return 
				}
			}
			if(res.type == 4){
				this.$Api.msg('请扫库区、货架二维码')
				return 
			}
			
			this.keyword = res.info.number
			this.getlist()
		})
	//}
  }
}
</script>
<style lang="less">
.popup-content{
	height: 300rpx;
	padding-bottom:100rpx;
}
</style>